package com.jt.Controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jt.Service.UserService;
import com.jt.pojo.User;
import com.jt.service.DubboUserService;


import com.jt.util.CookieUtil;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.JedisCluster;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/user")
public class UserController {
    @RequestMapping("/{moduleName}")
    public String module(@PathVariable String moduleName){
        return moduleName;
    }
    @Autowired
    private UserService userService;
    @Reference(check = false,timeout = 3000)
    private DubboUserService dubboUserService;
    @Autowired
    private JedisCluster jedisCluster;
@RequestMapping("/findUserById/{userId}")
@ResponseBody
    public User findUserById(@PathVariable Long userId){
return userService.findUserById(userId);
    }
/**
 * 完成用户注册操作
 * url地址：http://ww.jt.com/user/doRegister
 *参数：
 * 返回值：SysResult对象 返回的是JSON串
 * 业务说明：通过dubbo框架将userRPC传入jt-sso实现数据的入库操作
 * @return
 */
@RequestMapping("/doRegister")
@ResponseBody
public SysResult SaveUser(User user){
    dubboUserService.saveUser(user);
    return SysResult.success();
}

    /**
     *
     * @param user
     * @return
     */
    @RequestMapping("/doLogin")
    @ResponseBody
    public SysResult doLogin(User user, HttpServletResponse response){
    //1.实现用户的登录操作
        String ticket = dubboUserService.doLogin(user);//
        if(StringUtils.isEmpty(ticket)){
            return SysResult.fail();
        }
        Cookie cookie = new Cookie("JT_TICKET",ticket);
        cookie.setMaxAge(7*24*3600);
        cookie.setDomain("jt.com");
        cookie.setPath("/");
        response.addCookie(cookie);
        return SysResult.success();
}
    @RequestMapping("/logout")
    public String logout(HttpServletRequest request, HttpServletResponse response) {
        String ticket = CookieUtil.getCookieValue("JT_TICKET", request);
        if (!StringUtils.isEmpty(ticket)) {
            //1.删除redis
            jedisCluster.del(ticket);
            //2.删除cookie
            CookieUtil.deleteCookie("JT_TICKET", "/", "jt.com", response);
        }
        return "redirect:/";
        /* Cookie[] cookies = request.getCookies();
        if (cookies != null && cookies.length > 0) {
            for (Cookie cookie : cookies) {
                if ("JT_TICKET".equals(cookie.getName())) {
                    //
                    String ticket = cookie.getValue();
                    //
                    jedisCluster.del(ticket);
                    //
                    cookie.setDomain("jt.com");
                    cookie.setPath("/");
                    cookie.setMaxAge(0);
                    response.addCookie(cookie);
                    break;
                }
            }
        }
        return "redirect:/";
    }*/
    }
}
